var api = require('../../util/api.js');
Page({
    data: {
        contestName: "修改考核管理",
        examineId: null,
        examineStatus: 0,
        examineInfo: null
    },
    onLoad: function () {
        var _this = this;
        let chain = api.chain();
        api.get(`${api.url.examine}/${chain.examineId}`).then(res => {
            _this.setData({
                examineId: chain.examineId,
                examineInfo: res.data,
                examineStatus: res.data.status
            });
        })
    },
    updateContest: function () {
        var t = this.data.examineInfo.id;
        wx.navigateTo({
            url: "../../lhjd/examine/update?examineId=" + t
        });
    },
    start: function () {
        var _this = this;
        api.put(api.url.examine, {
            id: _this.data.examineInfo.id,
            status: 1
        }).then(res => {
            wx.showToast({
                title: "启动成功",
                icon: 'success'
            })
        })
    },
    end: function () {
        var _this = this;
        api.put(api.url.examine, {
            id: _this.data.examineInfo.id,
            status: 2
        }).then(res => {
            wx.showToast({
                title: "结束成功",
                icon: 'success'
            })
        })
    },
    delete: function () {
        var _this = this;
        wx.showModal({
            title: "温馨提示",
            content: "是否进行删除？删除后无法恢复，是否确定？",
            success: function (n) {
                if (n.confirm) {
                    api.del(`${api.url.examine}/${_this.data.examineInfo.id}`).then(res => {
                        api.back('删除成功','success');
                    })
                }
            }
        });
    },
    importExcel(e) {
        var _this = this;
        let examineId = api.chain().examineId;
        let examineStatus = api.chain().examineStatus;
        if (examineStatus == 0) {
            wx.showToast({
                title: '考核还未开始',
                icon: 'none'
            })
            return;
        }
        if (examineStatus == 2) {
            wx.showToast({
                title: '考核已经结束',
                icon: 'none'
            })
            return;
        }
        let tbId = e.currentTarget.dataset.tbId;
        var url = '';
        switch(tbId){
            case "125": url = api.url.ljjd.xcglList; break;
            case "126": url = api.url.ljjd.zhkhList; break;
            case "127": url = api.url.ljjd.ldjlList; break;
            case "128": url = api.url.ljjd.wtmxList; break;
            case "129": url = api.url.ljjd.sgdwpfList; break;
        }
        api.get(url, {
            examineId: examineId
        }).then(res => {
            if (res.rows.length < 1) {
                _this.showImport(examineId, tbId);
            } else {
                wx.showToast({
                    title: "数据已经存在了",
                    icon: 'none'
                })
            }
        })
    },
    showImport(examineId, tbId) {
        wx.showModal({
            title: "温馨提示",
            content: "目前没有数据，是否导入excel？",
            success: function (n) {
                if (n.confirm) {
                    wx.chooseMessageFile({
                        count: 1,
                        type: 'file',
                        extension: ['xls', 'xlsx'],
                        success(res) {
                            wx.uploadFile({
                                url: api.url.importXls,
                                filePath: res.tempFiles[0].path,
                                name: 'file',
                                formData: {
                                    tbId: tbId,
                                    examineId: examineId
                                },
                                header: {
                                    'Authorization': api.getToken(),
                                },
                                success(res) {
                                    var data = JSON.parse(res.data);
                                    wx.showModal({
                                        title: "导入日志",
                                        content: data.msg,
                                        showCancel: false
                                    });
                                }
                            })
                        }
                    })

                }
            }
        });
    }
});